﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using System.Web;
using System.Web.Services;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;
using MicroFormatProcessing;

namespace MicroFormatWebRole
{
    /// <summary>
    /// Zusammenfassungsbeschreibung für PluginService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // Um das Aufrufen dieses Webdiensts aus einem Skript mit ASP.NET AJAX zuzulassen, heben Sie die Auskommentierung der folgenden Zeile auf. 
    // [System.Web.Script.Services.ScriptService]
    public class PluginService : System.Web.Services.WebService
    {
        [WebMethod]
        public string Login(string userId, string password)
        {
            string status = null;
            Session mfSession = null;
            switch (UserUtil.CheckUser(userId, password))
            {
                case UserUtil.OK:
                    status = "ok";
                    mfSession = UserUtil.CreateSession(userId);
                    break;
                case UserUtil.UNKNOWN_USER:
                    status = "unknownUser";
                    break;
                case UserUtil.WRONG_PASSWORD:
                    status = "wrongPassword";
                    break;
            }
            string sessionId = (mfSession != null) ? mfSession.GetSessionId() : "";
            return String.Format("{0};{1}", status, sessionId);
        }

        [WebMethod]
        public string Remember(string mfSessionId, string mfData)
        {
            if ((mfSessionId != null) && (mfSessionId.Length > 0) && (mfData != null) && (mfData.Length > 0))
            {
                Session mfSession = UserUtil.GetSesssion(mfSessionId);
                if (mfSession != null)
                {
                    MicroFormatUtil.StoreMicroFormats(mfSession.UserId, mfData);
                    return "ok";
                }
                else
                {
                    return "invalidSession";
                }
            }
            else
            {
                return "missingParameters";
            }
        }

        [WebMethod]
        public string ClearData(string secret)
        {
            if ("ymndt".Equals(secret))
            {
                CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
                CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

                tableClient.DeleteTableIfExist("MfUsers");
                tableClient.DeleteTableIfExist("MfSessions");

                tableClient.DeleteTableIfExist("MicroFormats");
                tableClient.DeleteTableIfExist("Tags");
                tableClient.CreateTable("MicroFormats");
                tableClient.CreateTable("Tags");

                return "ok";
            }
            return "notAllowed";
        }
    }
}